package com.mycjj.android.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.activity.CjjMapActivity;
import com.mycjj.android.activity.Point_CommentActivity;
import com.mycjj.android.activity.Point_SelectAdviserActivity;
import com.mycjj.android.activity.Point_SelectYuyueTypeActivity;
import com.mycjj.android.activity.SelectStoreActivity;
import com.mycjj.android.activity.YuyueActivity;
import com.mycjj.android.bean.Adviser;
import com.mycjj.android.bean.LoginMemberBean;
import com.mycjj.android.util.Preferences;
import com.mycjj.android.widget.TabPageIndicator;


public class PointFragment extends BaseFragment {

	private ImageView mHeaderLeftButton;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;
	
	ViewPager pager;
	TabPageIndicator indicator;
	
	
	private TextView tv_fuwudian_menu2,tv_fuwudian_menu4;
	private static final String[] CONTENT = new String[] {"活动", "资讯"};


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = (View) inflater.inflate(R.layout.fragment_service_point, null);
		init(view);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
	}
	private TextView tv_yy,tv_adv;
	private TextView tv_store_name;
	private void init(View view) {
		
		mHeaderLeftButton = (ImageView)view.findViewById(R.id.header_left_button);
		mHeaderLeftButton.setVisibility(View.GONE);
		mHeaderRightButton = (Button) view.findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) view.findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("服务点");
		TextView item_view = (TextView)view.findViewById(R.id.tv_other_fuwudian);
		item_view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getActivity(), SelectStoreActivity.class);
				intent.putExtra("action", 0);
				startActivity(intent);
			}
		});
		
		tv_store_name = (TextView)view.findViewById(R.id.tv_store_name);
		
		tv_yy = (TextView)view.findViewById(R.id.tv_fuwudian_menu1);
		tv_yy.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getActivity(), Point_SelectYuyueTypeActivity.class));
			}
		});
		
		tv_adv = (TextView)view.findViewById(R.id.tv_fuwudian_menu3);
		tv_adv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				if(dataInstance.isStoreChange()){
					intent.setClass(getActivity(), Point_SelectAdviserActivity.class);
				}else{
					try{
//						Adviser adviser = dataInstance.getLoginMemberBean().getData().getMember_cars().getAdviser_info();
						Adviser adviser = dataInstance.getCarInfoBaseBean().getData().getAdviser();
						if(adviser == null){
							intent.setClass(getActivity(), Point_SelectAdviserActivity.class);
						}else{
							intent.setClass(getActivity(), Point_CommentActivity.class);
							intent.putExtra("type", 1);
						}
					}catch(Exception e){
						intent.setClass(getActivity(), Point_SelectAdviserActivity.class);
						e.printStackTrace();
					}
					
				}
				startActivity(intent);
				
			}
		});
		pager = (ViewPager)view.findViewById(R.id.pager);
		indicator = (TabPageIndicator)view.findViewById(R.id.indicator);
		
		tv_fuwudian_menu2 = (TextView)view.findViewById(R.id.tv_fuwudian_menu2);
		
		tv_fuwudian_menu2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), CjjMapActivity.class);
				startActivity(intent);
			}
		});
		
		tv_fuwudian_menu4 = (TextView)view.findViewById(R.id.tv_fuwudian_menu4);
		
		tv_fuwudian_menu4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				callPhone();
			}
		});
		
		

	}
	
	@Override
	public void onResume() {
		super.onResume();
		tv_store_name.setText(Preferences.getSharedPreferences(mApp, "storeName", ""));
		initData();// 请求数据
	}

	private void initData() {
		FragmentPagerAdapter adapter = new ServicePointAdapter(getChildFragmentManager());

        pager.setAdapter(adapter);
        
        indicator.setViewPager(pager);
        
        
	}
	
	
	private void callPhone(){
		LoginMemberBean bean = ProjectDataManage.getInstance().getLoginMemberBean();
		if(bean == null) return;
		
		final String phone_number = Preferences.getSharedPreferences(getActivity(), "storeTel", "");
			
		AlertDialog.Builder bu = new AlertDialog.Builder(getActivity());
		LayoutInflater layinInflater = LayoutInflater.from(getActivity());
		View viewphone = layinInflater.inflate(R.layout.store_show_phone_dialog, null);
		TextView determine = (TextView) viewphone.findViewById(R.id.determine);
		TextView showPhone = (TextView) viewphone.findViewById(R.id.showphone);
		if (TextUtils.isEmpty(phone_number)) {
			determine.setText("提示");
			showPhone.setText("该店无电话");
			bu.setView(viewphone);
			bu.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface arg0, int whitch) {
					arg0.dismiss();
				}
			});
			bu.show();
		} else {

			determine.setText("确定");
			showPhone.setText(phone_number);
			bu.setView(viewphone);
			bu.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface arg0, int whitch) {
					Intent in = new Intent();
					in.setAction(Intent.ACTION_CALL);
					Uri uri = Uri.parse("tel:" + phone_number);
					in.setData(uri);
					startActivity(in);
				}
			});
			bu.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface arg0, int whitch) {
				}
			});
			bu.show();
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}
	private Fragment [] FRAGMENT_ARRAY = {ServiceActivityFragment.newInstance(),ServiceArticleFragment.newInstance()};
	
	class ServicePointAdapter extends FragmentPagerAdapter {
        public ServicePointAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return FRAGMENT_ARRAY[position];
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

        @Override
        public int getCount() {
          return CONTENT.length;
        }
    }
}
